package com.barunsw.bwf.bims.tool;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Vector;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import org.apache.log4j.Logger;

public class ChannelDataInsertPanel extends JPanel {
	private static final long serialVersionUID 	= -4145480935515153082L;
	private static Logger LOGGER 				= Logger.getLogger(ChannelDataInsertPanel.class);
	
	private JPanel jPanel_Main 		= new JPanel();
	private JPanel jPanel_Input		= new JPanel();
	
	private JLabel jLabel_DeviceId 	= new JLabel();
	private JLabel jLabel_ChannelId = new JLabel();
	private JLabel jLabel_Duration 	= new JLabel();
	private JLabel jLabel_MinValue 	= new JLabel();
	private JLabel jLabel_MaxValue 	= new JLabel();
	
	private JTextField jTextField_DeviceId 	= new JTextField();
	private JTextField jTextField_ChannelId = new JTextField();
	private JTextField jTextField_Duration 	= new JTextField();
	private JTextField jTextField_MinValue 	= new JTextField();
	private JTextField jTextField_MaxValue 	= new JTextField();
	
	private GridBagLayout gridBagLayout = new GridBagLayout();
	
	public ChannelDataInsertPanel() {
		try {
			initComponent();
			initLabel();
			initTextField();
			initAction();
		}
		catch ( Exception ex ) {
			LOGGER.error(ex.getMessage(), ex);
		}
	}
	
	private void initComponent() {
		this.setLayout(gridBagLayout);
		this.setSize(new Dimension(300, 400));
		
		jPanel_Main.setLayout(gridBagLayout);
		jPanel_Input.setLayout(gridBagLayout);
		jPanel_Input.add(jLabel_DeviceId,
				new GridBagConstraints(0, 0, 1, 1, 
						0.0, 0.0, 
						GridBagConstraints.CENTER, GridBagConstraints.BOTH, 
						new Insets(5, 5, 5, 5), 
						0, 0));
		
		jPanel_Input.add(jTextField_DeviceId,
				new GridBagConstraints(1, 0, 1, 1, 
						1.0, 0.0, 
						GridBagConstraints.CENTER, GridBagConstraints.BOTH, 
						new Insets(5, 5, 5, 5), 
						0, 0));
		
		jPanel_Input.add(jLabel_ChannelId,
				new GridBagConstraints(0, 1, 1, 1, 
						0.0, 0.0, 
						GridBagConstraints.CENTER, GridBagConstraints.BOTH, 
						new Insets(5, 5, 5, 5), 
						0, 0));
		
		jPanel_Input.add(jTextField_ChannelId,
				new GridBagConstraints(1, 1, 1, 1, 
						1.0, 0.0, 
						GridBagConstraints.CENTER, GridBagConstraints.BOTH, 
						new Insets(5, 5, 5, 5), 
						0, 0));
		
		/*
		jPanel_Input.add(jLabel_Duration,
				new GridBagConstraints(0, 2, 1, 1, 
						0.0, 0.0, 
						GridBagConstraints.CENTER, GridBagConstraints.BOTH, 
						new Insets(5, 5, 5, 5), 
						0, 0));
		
		jPanel_Input.add(jTextField_Duration,
				new GridBagConstraints(1, 2, 1, 1, 
						1.0, 0.0, 
						GridBagConstraints.CENTER, GridBagConstraints.BOTH, 
						new Insets(5, 5, 5, 5), 
						0, 0));
		*/
		
		jPanel_Input.add(jLabel_MinValue,
				new GridBagConstraints(0, 2, 1, 1, 
						0.0, 0.0, 
						GridBagConstraints.CENTER, GridBagConstraints.BOTH, 
						new Insets(5, 5, 5, 5), 
						0, 0));
		
		jPanel_Input.add(jTextField_MinValue,
				new GridBagConstraints(1, 2, 1, 1, 
						1.0, 0.0, 
						GridBagConstraints.CENTER, GridBagConstraints.BOTH, 
						new Insets(5, 5, 5, 5), 
						0, 0));
		
		jPanel_Input.add(jLabel_MaxValue,
				new GridBagConstraints(0, 3, 1, 1, 
						0.0, 0.0, 
						GridBagConstraints.CENTER, GridBagConstraints.BOTH, 
						new Insets(5, 5, 5, 5), 
						0, 0));
		
		jPanel_Input.add(jTextField_MaxValue,
				new GridBagConstraints(1, 3, 1, 1, 
						1.0, 0.0, 
						GridBagConstraints.CENTER, GridBagConstraints.BOTH, 
						new Insets(5, 5, 5, 5), 
						0, 0));
		
		jPanel_Main.add(jPanel_Input,
				new GridBagConstraints(0, 0, 1, 1, 
						1.0, 1.0, 
						GridBagConstraints.CENTER, GridBagConstraints.BOTH, 
						new Insets(0, 0, 0, 0), 
						0, 0));
		
		this.add(jPanel_Main,
				new GridBagConstraints(0, 0, 1, 1, 
						1.0, 1.0, 
						GridBagConstraints.CENTER, GridBagConstraints.BOTH, 
						new Insets(0, 0, 0, 0), 
						0, 0));
	}
	
	private void initLabel() {
		jLabel_DeviceId.setText("DEVICE ID");
		jLabel_ChannelId.setText("CHANNEL ID");
		jLabel_Duration.setText("DURATION(s)");
		jLabel_MinValue.setText("MINIMUM VALUE");
		jLabel_MaxValue.setText("MAXIMUM VALUE");
		
		jLabel_DeviceId.setHorizontalAlignment(SwingConstants.RIGHT);
		jLabel_ChannelId.setHorizontalAlignment(SwingConstants.RIGHT);
		jLabel_Duration.setHorizontalAlignment(SwingConstants.RIGHT);
		jLabel_MinValue.setHorizontalAlignment(SwingConstants.RIGHT);
		jLabel_MaxValue.setHorizontalAlignment(SwingConstants.RIGHT);
	}
	
	private void initTextField() {
		jTextField_DeviceId.setPreferredSize(new Dimension(100, 22));
		jTextField_DeviceId.setMaximumSize(new Dimension(100, 22));
		jTextField_DeviceId.setMinimumSize(new Dimension(100, 22));
		
		jTextField_ChannelId.setPreferredSize(new Dimension(100, 22));
		jTextField_ChannelId.setMaximumSize(new Dimension(100, 22));
		jTextField_ChannelId.setMinimumSize(new Dimension(100, 22));
		
		jTextField_Duration.setPreferredSize(new Dimension(100, 22));
		jTextField_Duration.setMaximumSize(new Dimension(100, 22));
		jTextField_Duration.setMinimumSize(new Dimension(100, 22));
		
		jTextField_MinValue.setPreferredSize(new Dimension(100, 22));
		jTextField_MinValue.setMaximumSize(new Dimension(100, 22));
		jTextField_MinValue.setMinimumSize(new Dimension(100, 22));
		
		jTextField_MaxValue.setPreferredSize(new Dimension(100, 22));
		jTextField_MaxValue.setMaximumSize(new Dimension(100, 22));
		jTextField_MaxValue.setMinimumSize(new Dimension(100, 22));
		
		jTextField_DeviceId.setHorizontalAlignment(SwingConstants.RIGHT);
		jTextField_ChannelId.setHorizontalAlignment(SwingConstants.RIGHT);
		jTextField_Duration.setHorizontalAlignment(SwingConstants.RIGHT);
		jTextField_MinValue.setHorizontalAlignment(SwingConstants.RIGHT);
		jTextField_MaxValue.setHorizontalAlignment(SwingConstants.RIGHT);
	}
	
	private void initAction() {
		jTextField_DeviceId.addKeyListener(new ChannelDataInsertPanel_jTextField_KeyListener(this));
		jTextField_ChannelId.addKeyListener(new ChannelDataInsertPanel_jTextField_KeyListener(this));
		jTextField_Duration.addKeyListener(new ChannelDataInsertPanel_jTextField_KeyListener(this));
		jTextField_MinValue.addKeyListener(new ChannelDataInsertPanel_jTextField_KeyListener(this));
		jTextField_MaxValue.addKeyListener(new ChannelDataInsertPanel_jTextField_KeyListener(this));
	}
	
	public Vector<String> getDataVector() {
		Vector<String> dataVector = new Vector<String>();
		
		String deviceId 	= jTextField_DeviceId.getText();
		String channelId 	= jTextField_ChannelId.getText();
		String minValue 	= jTextField_MinValue.getText();
		String maxValue 	= jTextField_MaxValue.getText();
		
		if ( deviceId.equals("") || channelId.equals("") ||	minValue.equals("") || maxValue.equals("") ) {
			if ( deviceId.equals("") ) {
				jTextField_DeviceId.requestFocus();
			}
			else if ( channelId.equals("") ) {
				jTextField_ChannelId.requestFocus();
			}
			else if ( minValue.equals("") ) {
				jTextField_MinValue.requestFocus();
			}
			else if ( maxValue.equals("") ) {
				jTextField_MaxValue.requestFocus();
			}
			
			return null;
		}
		else {
			//dataVector.addElement(deviceId);
			dataVector.addElement(channelId);
			dataVector.addElement("POST");
			dataVector.addElement("http://localhost:8080/bwf/channel_history/" + channelId);
			dataVector.addElement(minValue);
			dataVector.addElement(maxValue);
			
			return dataVector;
		}
	}
	
	public void jTextField_keyTyped(KeyEvent e) {
		char inputChar			= e.getKeyChar();
		int keyCode				= e.getKeyCode();
		
		if ( inputChar != KeyEvent.VK_BACK_SPACE ) {
			if ( !(Character.isDigit(inputChar) || keyCode == 37 || keyCode == 39) ) {
				getToolkit().beep();
				e.consume();
			}
		}
	}
}

class ChannelDataInsertPanel_jTextField_KeyListener extends KeyAdapter {
	private ChannelDataInsertPanel adaptee;
	
	public ChannelDataInsertPanel_jTextField_KeyListener(ChannelDataInsertPanel adaptee) {
		this.adaptee = adaptee;
	}
	
	@Override
	public void keyTyped(KeyEvent e) {
		adaptee.jTextField_keyTyped(e);
	}
}
